From e21940f7f180dc673c7d9c0282b060281494a690 Mon Sep 17 00:00:00 2001 From: "Karl O. Pinc" Date: Mon, 31 Aug 2020 17:01:26 -0500 Subject: [PATCH] Move exceptions into their own module --- src/pgwui_upload/check_settings.py | 15 +----- src/pgwui_upload/exceptions.py | 74 ++++++++++++++++++++++++++++++ src/pgwui_upload/views/upload.py | 49 ++++---------------- tests/test_check_settings.py | 3 +- 4 files changed, 86 insertions(+), 55 deletions(-) create mode 100644 src/pgwui_upload/exceptions.py diff --git a/src/pgwui_upload/check_settings.py b/src/pgwui_upload/check_settings.py index 330d943..1b725dd 100644 --- a/src/pgwui_upload/check_settings.py +++ b/src/pgwui_upload/check_settings.py @@ -19,8 +19,8 @@ # Karl O. Pinc -from pgwui_common import exceptions as common_ex from pgwui_common import checkset +from . import exceptions as upload_ex PGWUI_COMPONENT = 'pgwui_upload' @@ -31,17 +31,6 @@ REQUIRED_SETTINGS = [] BOOLEAN_SETTINGS = [] -class UploadError(common_ex.Error): - pass - - -class BadLiteralColumnHeadingsError(UploadError): - def __init__(self, value): - super().__init__( - 'The "pgwui.literal_column_headings" PGWUI setting must be' - '"on", "off", "ask", or not present') - - def validate_literal_column_headings(errors, settings): '''Make sure the values are those allowed ''' @@ -49,7 +38,7 @@ def validate_literal_column_headings(errors, settings): if value is None: return if value not in ('on', 'off', 'ask'): - errors.append(BadLiteralColumnHeadingsError(value)) + errors.append(upload_ex.BadLiteralColumnHeadingsError(value)) def check_settings(component_config): diff --git a/src/pgwui_upload/exceptions.py b/src/pgwui_upload/exceptions.py new file mode 100644 index 0000000..d893ae8 --- /dev/null +++ b/src/pgwui_upload/exceptions.py @@ -0,0 +1,74 @@ +# Copyright (C) 2020 The Meme Factory, Inc. http://www.karlpinc.com/ + +# This file is part of PGWUI_Upload. +# +# This program is free software: you can redistribute it and/or +# modify it under the terms of the GNU Affero General Public License +# as published by the Free Software Foundation, either version 3 of +# the License, or (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU +# Affero General Public License for more details. +# +# You should have received a copy of the GNU Affero General Public +# License along with this program. If not, see +# . +# + +# Karl O. Pinc + +from pgwui_common import exceptions as common_ex +from pgwui_core import exceptions as core_ex + + +# PGWUI setting related exceptions + +class UploadError(common_ex.Error): + pass + + +class BadLiteralColumnHeadingsError(UploadError): + def __init__(self, value): + super().__init__( + 'The "pgwui.literal_column_headings" PGWUI setting must be' + '"on", "off", "ask", or not present') + + +# Upload related exceptions + +class NoTableError(core_ex.PGWUIError): + '''No table uploaded''' + def __init__(self, e, descr='', detail=''): + super(NoTableError, self).__init__(e, descr, detail) + + +class BadTableError(core_ex.PGWUIError): + '''Supplied name does not work for a table or view''' + def __init__(self, e, descr='', detail=''): + super(BadTableError, self).__init__(e, descr, detail) + + +class MissingTableError(BadTableError): + '''The supplied table or view does not exist''' + def __init__(self, e, descr='', detail=''): + super(MissingTableError, self).__init__(e, descr, detail) + + +class MissingSchemaError(BadTableError): + '''The schema portion of the supplied table or view does not exist''' + def __init__(self, e, descr='', detail=''): + super(MissingSchemaError, self).__init__(e, descr, detail) + + +class CannotInsertError(BadTableError): + '''Cannot insert into the supplied table or view''' + def __init__(self, e, descr='', detail=''): + super(CannotInsertError, self).__init__(e, descr, detail) + + +class BadHeadersError(core_ex.PGWUIError): + '''The headers in the uploaded file are bad.''' + def __init__(self, e, descr='', detail=''): + super(BadHeadersError, self).__init__(e, descr, detail) diff --git a/src/pgwui_upload/views/upload.py b/src/pgwui_upload/views/upload.py index a0d63fa..b571818 100644 --- a/src/pgwui_upload/views/upload.py +++ b/src/pgwui_upload/views/upload.py @@ -36,7 +36,6 @@ import psycopg2.errorcodes from psycopg2 import ProgrammingError from pgwui_common import auth_base_view -from pgwui_core.exceptions import PGWUIError from pgwui_core.core import ( UploadEngine, DataLineProcessor, @@ -48,43 +47,10 @@ from pgwui_core.core import ( is_checked, ) -log = logging.getLogger(__name__) - - -class NoTableError(PGWUIError): - '''No table uploaded''' - def __init__(self, e, descr='', detail=''): - super(NoTableError, self).__init__(e, descr, detail) - - -class BadTableError(PGWUIError): - '''Supplied name does not work for a table or view''' - def __init__(self, e, descr='', detail=''): - super(BadTableError, self).__init__(e, descr, detail) - +from pgwui_upload import exceptions as upload_ex -class MissingTableError(BadTableError): - '''The supplied table or view does not exist''' - def __init__(self, e, descr='', detail=''): - super(MissingTableError, self).__init__(e, descr, detail) - -class MissingSchemaError(BadTableError): - '''The schema portion of the supplied table or view does not exist''' - def __init__(self, e, descr='', detail=''): - super(MissingSchemaError, self).__init__(e, descr, detail) - - -class CannotInsertError(BadTableError): - '''Cannot insert into the supplied table or view''' - def __init__(self, e, descr='', detail=''): - super(CannotInsertError, self).__init__(e, descr, detail) - - -class BadHeadersError(PGWUIError): - '''The headers in the uploaded file are bad.''' - def __init__(self, e, descr='', detail=''): - super(BadHeadersError, self).__init__(e, descr, detail) +log = logging.getLogger(__name__) class SaveLine(DataLineProcessor): @@ -146,7 +112,8 @@ class TableUploadHandler(TabularFileUploadHandler): qualified_table = uf['table'] if qualified_table == '': - errors.append(NoTableError('No table or view name supplied')) + errors.append(upload_ex.NoTableError( + 'No table or view name supplied')) self.double_validator(errors) @@ -173,11 +140,11 @@ class TableUploadHandler(TabularFileUploadHandler): except ProgrammingError as err: pgcode = err.pgcode if pgcode == psycopg2.errorcodes.INVALID_SCHEMA_NAME: - raise MissingSchemaError( + raise upload_ex.MissingSchemaError( 'No such schema', err.diag.message_primary,) elif pgcode == psycopg2.errorcodes.UNDEFINED_TABLE: - raise MissingTableError( + raise upload_ex.MissingTableError( 'No such table or view', err.diag.message_primary, ('

Hint: Check spelling or try qualifying the' @@ -236,7 +203,7 @@ class TableUploadHandler(TabularFileUploadHandler): schema, table = self.resolve_table(qualified_table) if not self.good_table(schema, table): - raise CannotInsertError( + raise upload_ex.CannotInsertError( 'Cannot insert into supplied table or view', ('({0}) is either is a view' ' that cannot be inserted into' @@ -284,7 +251,7 @@ class TableUploadHandler(TabularFileUploadHandler): for bad_col in bad_cols: detail += '

  • {0}
  • '.format(markupsafe.escape(bad_col)) detail += '' - raise BadHeadersError( + raise upload_ex.BadHeadersError( 'Header line contains unknown column names', detail=detail) diff --git a/tests/test_check_settings.py b/tests/test_check_settings.py index 330508c..d6aac1d 100644 --- a/tests/test_check_settings.py +++ b/tests/test_check_settings.py @@ -23,6 +23,7 @@ import pgwui_upload.check_settings as check_settings from pgwui_common import checkset from pgwui_testing import testing +from pgwui_upload import exceptions as upload_ex # Activiate our pytest plugin pytest_plugins = ("pgwui",) @@ -95,7 +96,7 @@ def test_validate_literal_column_headings_bad(): assert errors assert isinstance( - errors[0], check_settings.BadLiteralColumnHeadingsError) + errors[0], upload_ex.BadLiteralColumnHeadingsError) literal_err = 'literal column headings error' -- 2.34.1